//
// Created by 12992 on 24-10-15.
//

// You may need to build the project (run Qt uic code generator) to get "ui_VideoPreview.h" resolved

#include "videopreview.h"
#include "ui_VideoPreview.h"
#include "ImageWidget.h"
#include <QLabel>

namespace gui::widgets
{
    VideoPreview::VideoPreview(QWidget* parent) :
        QWidget(parent), ui(new Ui::VideoPreview)
    {
        ui->setupUi(this);
        this->init_without_image();
        this->init_signals();
    }

    VideoPreview::~VideoPreview()
    {
        delete ui;
    }

    VideoPreview::VideoPreview(const QVector<QImage>& images, QWidget* parent) :
        QWidget(parent), ui(new Ui::VideoPreview)
    {
        ui->setupUi(this);
        if (images.isEmpty())
        {
            this->init_without_image();
        }
        else
        {
            this->init_with_image(images);
        }
        this->init_signals();
    }

    void VideoPreview::init_without_image()
    {
        this->is_empty = true;
        this->new_image(QImage(":/image/noPreview.png"));
        this->ui->stackedWidget->setCurrentIndex(this->current_index);
    }

    void VideoPreview::new_image(const QImage& image)
    {
        auto label = new ImageWidget(image, this->ui->stackedWidget);
        this->ui->stackedWidget->addWidget(label);
    }

    void VideoPreview::addImage(const QImage& image)
    {
        if (this->is_empty)
        {
            auto widget = this->ui->stackedWidget->widget(0);
            qDebug() << this->ui->stackedWidget->count();
            this->ui->stackedWidget->removeWidget(widget);
            delete widget;
            this->is_empty = false;
            qDebug() << this->ui->stackedWidget->count();
        }
        this->new_image(image);
    }

    void VideoPreview::clear()
    {
        QVector<QWidget*> widgets;
        for (int i = 0; i < this->ui->stackedWidget->count(); i++)
        {
            widgets.push_back(this->ui->stackedWidget->widget(i));
        }
        for (auto widget : widgets)
        {
            this->ui->stackedWidget->removeWidget(widget);
            delete widget;
        }
    }

    void VideoPreview::init_with_image(const QVector<QImage>& images)
    {
        for (const auto& image : images)
        {
            this->new_image(image);
        }
        this->ui->stackedWidget->setCurrentIndex(this->current_index);
    }

    void VideoPreview::init_signals()
    {
        connect(this->ui->nextButton, &QPushButton::clicked, this, &VideoPreview::nextPage);
        connect(this->ui->prevButton, &QPushButton::clicked, this, &VideoPreview::prevPage);
    }

    void VideoPreview::nextPage()
    {
        qDebug() << "next page";
        this->current_index++;
        if (this->current_index >= this->ui->stackedWidget->count())
        {
            this->current_index = this->ui->stackedWidget->count() - 1;
        }
        this->ui->stackedWidget->setCurrentIndex(this->current_index);
    }

    void VideoPreview::prevPage()
    {
        qDebug() << "prev page";
        this->current_index--;
        if (this->current_index < 0)
        {
            this->current_index = 0;
        }
        this->ui->stackedWidget->setCurrentIndex(this->current_index);
    }
} // gui::widgets
